package com.neusoft.yiyang.customer.controller;

import com.neusoft.yiyang.common.entity.customer.LeaveRequest;
import com.neusoft.yiyang.customer.service.LeaveService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "退住申请管理接口", description = "负责客户退住申请的创建、查询、审批等全流程管理")
@RestController
@RequestMapping("/leave-requests")
public class LeaveController {

    @Autowired
    private LeaveService leaveService;

    @Operation(summary = "创建退住申请", description = "客户提交退住申请，包含退住原因、预计退住时间等信息")
    @PostMapping("createLeaveRequest")
    public Map<String, Object> createLeaveRequest(
            @Parameter(description = "退住申请信息（含客户ID、退住原因等）", required = true)
            @RequestBody LeaveRequest leaveRequest) {
        return leaveService.createLeaveRequest(leaveRequest);
    }

    @Operation(summary = "查询所有退住申请", description = "获取系统中所有退住申请及关联的客户、审批信息")
    @GetMapping
    public List<Map<String, Object>> getAllLeaveRequests() {
        return leaveService.getAllLeaveRequests();
    }

    @Operation(summary = "查询护工负责的退住申请", description = "获取指定护工所负责客户的退住申请列表")
    @GetMapping("/getByCaretakerId/{caretakerId}")
    public List<Map<String, Object>> getLeaveRequestsByCaretakerId(
            @Parameter(description = "护工ID", required = true) @PathVariable Integer caretakerId){
        return leaveService.getLeaveRequestsByCaretakerId(caretakerId);
    }

    @Operation(summary = "根据ID查询退住申请", description = "获取指定退住申请的详细信息")
    @GetMapping("/{leaveRequestId}")
    public LeaveRequest getLeaveRequestById(
            @Parameter(description = "退住申请ID", required = true) @PathVariable Integer leaveRequestId) {
        return leaveService.getLeaveRequestById(leaveRequestId);
    }

    @Operation(summary = "更新退住申请状态", description = "修改退住申请的审批状态（如待审批、已同意、已拒绝）")
    @PutMapping("/{leaveRequestId}/status")
    public void updateLeaveRequestStatus(
            @Parameter(description = "退住申请ID", required = true) @PathVariable Integer leaveRequestId,
            @Parameter(description = "审批状态（如pending/agreed/rejected）", required = true) @RequestParam String status) {
        leaveService.updateLeaveRequestStatus(leaveRequestId, status);
    }

    @Operation(summary = "拒绝退住申请", description = "将退住申请状态设为“已拒绝”")
    @GetMapping("/reject/{leaveRequestId}")
    public void rejectLeaveRequest(
            @Parameter(description = "退住申请ID", required = true) @PathVariable Integer leaveRequestId) {
        leaveService.rejectLeaveRequest(leaveRequestId);
    }

    @Operation(summary = "同意退住申请", description = "将退住申请状态设为“已同意”，触发后续退住流程")
    @GetMapping("/agree/{leaveRequestId}")
    public void agreeLeaveRequest(
            @Parameter(description = "退住申请ID", required = true) @PathVariable Integer leaveRequestId) {
        leaveService.agreeLeaveRequest(leaveRequestId);
    }

    @Operation(summary = "按客户姓名搜索退住申请", description = "根据客户姓名模糊查询相关的退住申请")
    @GetMapping("search/{customerName}")
    public List<Map<String, Object>> searchByCustomerName(
            @Parameter(description = "客户姓名（支持模糊匹配）", required = true) @PathVariable String customerName) {
        return leaveService.searchByCustomerName(customerName);
    }

    @Operation(summary = "删除退住申请", description = "移除指定的退住申请记录")
    @DeleteMapping("/{leaveRequestId}")
    public void deletLeaveRequest(
            @Parameter(description = "退住申请ID", required = true) @PathVariable Integer leaveRequestId) {
        leaveService.deletLeaveRequest(leaveRequestId);
    }

    @Operation(summary = "编辑退住申请", description = "修改退住申请的信息（如退住时间、原因等）")
    @PutMapping("/update/{leaveRequestId}")
    public void updateLeaveRequest(
            @Parameter(description = "退住申请ID", required = true) @PathVariable Integer leaveRequestId,
            @Parameter(description = "更新后的退住申请信息", required = true) @RequestBody LeaveRequest updateRequest) {
        leaveService.updateLeaveRequest(leaveRequestId, updateRequest);
    }
}